home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / demos / clearspd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-12  |  5.2 KB  |  233 lines

  1. /* clearspd.c */
  2.  
  3. /*
  4.  * Simple GLUT program to measure glClear() and glutSwapBuffers() speed.
  5.  * Brian Paul  February 15, 1997
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13. #include <sys/times.h>
  14. #include <sys/param.h>
  15.  
  16.  
  17.  
  18. /*
  19.  * Return system time in seconds.
  20.  * NOTE:  this implementation may not be very portable!
  21.  */
  22. GLdouble GetTime( void )
  23. {
  24.    static GLdouble prev_time = 0.0;
  25.    static GLdouble time;
  26.    struct tms tm;
  27.    clock_t clk;
  28.  
  29.    clk = times(&tm);
  30.  
  31. #ifdef CLK_TCK
  32.    time = (double)clk / (double)CLK_TCK;
  33. #else
  34.    time = (double)clk / (double)HZ;
  35. #endif
  36.  
  37.    if (time>prev_time) {
  38.       prev_time = time;
  39.       return time;
  40.    }
  41.    else {
  42.       return prev_time;
  43.    }
  44. }
  45.  
  46.  
  47. static float MinPeriod = 2.0;   /* 2 seconds */
  48. static int ColorMode = GLUT_RGB;
  49. static int Width = 400.0;
  50. static int Height = 400.0;
  51. static int Loops = 100;
  52. static int Size = 50;
  53. static float ClearColor = 0.0;
  54. static GLbitfield BufferMask = GL_COLOR_BUFFER_BIT;
  55. static GLboolean SwapFlag = GL_FALSE;
  56.  
  57.  
  58.  
  59. static void Idle( void )
  60. {
  61.    glutPostRedisplay();
  62. }
  63.  
  64.  
  65. static void Display( void )
  66. {
  67.    double t0, t1;
  68.    double clearRate;
  69.    double pixelRate;
  70.    int i;
  71.  
  72.    glClearColor( ClearColor, ClearColor, ClearColor, 0.0 );
  73.    ClearColor += 0.1;
  74.    if (ClearColor>1.0)
  75.       ClearColor = 0.0;
  76.  
  77.    if (SwapFlag) {
  78.       t0 = GetTime();
  79.       for (i=0;i<Loops;i++) {
  80.          glClear( BufferMask );
  81.          glutSwapBuffers();
  82.       }
  83.       t1 = GetTime();
  84.    }
  85.    else {
  86.       t0 = GetTime();
  87.       for (i=0;i<Loops;i++) {
  88.          glClear( BufferMask );
  89.       }
  90.       t1 = GetTime();
  91.       glutSwapBuffers();
  92.    }
  93.  
  94.    if (t1-t0 < MinPeriod) {
  95.       /* Next time do more clears to get longer elapsed time */
  96.       Loops *= 2;
  97.       return;
  98.    }
  99.  
  100.    clearRate = Loops / (t1-t0);
  101.    pixelRate = clearRate * Width * Height;
  102.    if (SwapFlag) {
  103.       printf("Rate: %d clears+swaps in %gs = %g clears+swaps/s   %d pixels/s\n",
  104.              Loops, t1-t0, clearRate, (int)pixelRate );
  105.    }
  106.    else {
  107.       printf("Rate: %d clears in %gs = %g clears/s   %d pixels/s\n",
  108.              Loops, t1-t0, clearRate, (int)pixelRate);
  109.    }
  110. }
  111.  
  112.  
  113. static void Reshape( int width, int height )
  114. {
  115.    Width = width;
  116.    Height = height;
  117.    glViewport( 0, 0, width, height );
  118.    glMatrixMode( GL_PROJECTION );
  119.    glLoadIdentity();
  120.    glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
  121.    glMatrixMode( GL_MODELVIEW );
  122.    glLoadIdentity();
  123. }
  124.  
  125.  
  126. static void Key( unsigned char key, int x, int y )
  127. {
  128.    switch (key) {
  129.       case 27:
  130.          exit(0);
  131.          break;
  132.    }
  133.    glutPostRedisplay();
  134. }
  135.  
  136.  
  137. static void Init( int argc, char *argv[] )
  138. {
  139.    GLint shade;
  140.  
  141.    int i;
  142.    for (i=1; i<argc; i++) {
  143.       if (strcmp(argv[i],"+rgb")==0)
  144.          ColorMode = GLUT_RGB;
  145.       else if (strcmp(argv[i],"+ci")==0)
  146.          ColorMode = GLUT_INDEX;
  147.       else if (strcmp(argv[i],"-color")==0)
  148.          BufferMask = 0;
  149.       else if (strcmp(argv[i],"+depth")==0)
  150.          BufferMask |= GL_DEPTH_BUFFER_BIT;
  151.       else if (strcmp(argv[i],"+alpha")==0)
  152.          ColorMode = GLUT_RGB | GLUT_ALPHA;
  153.       else if (strcmp(argv[i],"+stencil")==0)
  154.          BufferMask |= GL_STENCIL_BUFFER_BIT;
  155.       else if (strcmp(argv[i],"+accum")==0)
  156.          BufferMask |= GL_ACCUM_BUFFER_BIT;
  157.       else if (strcmp(argv[i],"-width")==0) {
  158.          Width = atoi(argv[i+1]);
  159.          i++;
  160.       }
  161.       else if (strcmp(argv[i],"-height")==0) {
  162.          Height = atoi(argv[i+1]);
  163.          i++;
  164.       }
  165.       else if (strcmp(argv[i],"+swap")==0) {
  166.          SwapFlag = GL_TRUE;
  167.       }
  168.       else if (strcmp(argv[i],"-swap")==0) {
  169.          SwapFlag = GL_FALSE;
  170.       }
  171.       else
  172.          printf("Unknown option: %s\n", argv[i]);
  173.    }
  174.  
  175.    if (ColorMode & GLUT_ALPHA)
  176.       printf("Mode:  RGB + Alpha\n");
  177.    else if (ColorMode==GLUT_RGB)
  178.       printf("Mode:  RGB\n");
  179.    else
  180.       printf("Mode:  Color Index\n");
  181.    printf("SwapBuffers: %s\n", SwapFlag ? "yes" : "no" );
  182.    printf("Size: %d x %d\n", Width, Height);
  183.    printf("Buffers: ");
  184.    if (BufferMask & GL_COLOR_BUFFER_BIT)  printf("color ");
  185.    if (BufferMask & GL_DEPTH_BUFFER_BIT)  printf("depth ");
  186.    if (BufferMask & GL_STENCIL_BUFFER_BIT)  printf("stencil ");
  187.    if (BufferMask & GL_ACCUM_BUFFER_BIT)  printf("accum ");
  188.    printf("\n");
  189. }
  190.  
  191.  
  192. static void Help( const char *program )
  193. {
  194.    printf("%s options:\n", program);
  195.    printf("  +rgb       RGB mode\n");
  196.    printf("  +ci        color index mode\n");
  197.    printf("  -color     don't clear color buffer\n");
  198.    printf("  +alpha     clear alpha buffer\n");
  199.    printf("  +depth     clear depth buffer\n");
  200.    printf("  +stencil   clear stencil buffer\n");
  201.    printf("  +accum     clear accum buffer\n");
  202.    printf("  +swap      also do SwapBuffers\n");
  203.    printf("  -swap      don't do SwapBuffers\n");
  204. }
  205.  
  206.  
  207. int main( int argc, char *argv[] )
  208. {
  209.    printf("For options:  %s -help\n", argv[0]);
  210.  
  211.    Init( argc, argv );
  212.  
  213.    glutInit( &argc, argv );
  214.    glutInitWindowSize( (int) Width, (int) Height );
  215.    glutInitWindowPosition( 0, 0 );
  216.  
  217.    glutInitDisplayMode( ColorMode | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_ACCUM );
  218.  
  219.    glutCreateWindow( argv[0] );
  220.  
  221.    if (argc==2 && strcmp(argv[1],"-help")==0) {
  222.       Help(argv[0]);
  223.       return 0;
  224.    }
  225.  
  226.    glutReshapeFunc( Reshape );
  227.    glutKeyboardFunc( Key );
  228.    glutDisplayFunc( Display );
  229.    glutIdleFunc( Idle );
  230.  
  231.    glutMainLoop();
  232. }
  233.